﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Xml;
using System.IO;
using System.Data;

namespace DAO
{
    public class CConnectXML
    {

        private string _Filename;

        #region KhoiTao

        public CConnectXML()
        {
            _Filename = "Data.xml";
        }

        public CConnectXML(string Filename)
        {
            _Filename = Filename;
        }

        #endregion

        #region Static
        
        #region LoadData

        public static DataTable LoadListKeys(string Filename)
        {
            DataSet ds = new DataSet();
            DataTable data = new DataTable();
            try
            {
                ds.ReadXml(Filename);
                data = ds.Tables["Keys"];
                data.Rows.RemoveAt(0);
            }
            catch
            {
                CKeys _Key = new CKeys();
                CreateNewTable(Filename);
                ds.ReadXml(Filename);
            }
            return data;
        }

        public static CKeys LoadKey(string ShortcutKey,String Filename)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Filename);
            XmlNode node = doc.DocumentElement;
            foreach (XmlNode node1 in node.ChildNodes)
            {
                foreach (XmlAttribute at in node1.Attributes)
                {
                    if (at.Name.Equals("Shortcutkey"))
                    {
                        if (at.InnerText.Equals(ShortcutKey))
                        {
                            return new CKeys(node1.Attributes[0].InnerText, node1.Attributes[1].InnerText, Convert.ToInt32(node1.Attributes[2].InnerText), Convert.ToInt32(node1.Attributes[3].InnerText), node1.Attributes[4].InnerText);
                        }
                    }
                }
            }
            return new CKeys();
        }

        #endregion

        #region ADD/Edit/Delete

        public static bool AddKey(CKeys NewKey, string Filename)
        {
            XmlDocument doc = new XmlDocument();
            FileInfo filename = new FileInfo(Filename);
            if (filename.Exists)
            {
                doc.Load(Filename);
                XmlNode node = doc.DocumentElement;
                XmlElement xmlk = doc.CreateElement("Keys");
                XmlAttribute xmlsk = doc.CreateAttribute("Shortcutkey");
                XmlAttribute xmlrk = doc.CreateAttribute("Runningkey");
                XmlAttribute xmldl = doc.CreateAttribute("Delay");
                XmlAttribute xmllo = doc.CreateAttribute("Loop");
                XmlAttribute xmldt = doc.CreateAttribute("Description");
                xmlsk.Value = NewKey.Shortcutkey;
                xmlk.Attributes.Append(xmlsk);
                xmlrk.Value = NewKey.Runningkey;
                xmlk.Attributes.Append(xmlrk);
                xmldl.Value = Convert.ToString(NewKey.Delay);
                xmlk.Attributes.Append(xmldl);
                xmllo.Value = Convert.ToString(NewKey.Loop);
                xmlk.Attributes.Append(xmllo);
                xmldt.Value = NewKey.Description;
                xmlk.Attributes.Append(xmldt);
                node.AppendChild(xmlk);
                doc.Save(Filename);
                return true;
            }
            return false;
        }

        public static bool EditKey(CKeys KeyNew, String ShortcutKeyOld, string Filename)
        {
            bool _kt = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(Filename);
            XmlNode node = doc.DocumentElement;
            foreach (XmlNode node1 in node.ChildNodes)
            {
                foreach (XmlAttribute at in node1.Attributes)
                {
                    if (at.Name.Equals("Shortcutkey"))
                    {
                        if (Convert.ToString(at.Value) == ShortcutKeyOld)
                        {
                            node1.Attributes[0].InnerText = KeyNew.Shortcutkey;
                            node1.Attributes[1].InnerText = KeyNew.Runningkey;
                            node1.Attributes[2].InnerText = Convert.ToString(KeyNew.Delay);
                            node1.Attributes[3].InnerText = Convert.ToString(KeyNew.Loop);
                            node1.Attributes[4].InnerText = KeyNew.Description;
                            _kt = true;
                        }
                    }
                }
            }
            doc.Save(Filename);
            return _kt;
        }

        public static bool DeleteKey(String ShortcutKey, string Filename)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Filename);
            XmlNode node = doc.DocumentElement;
            bool _kt = false;
            foreach (XmlNode node1 in node.ChildNodes)
            {
                foreach (XmlAttribute at in node1.Attributes)
                {
                    if (at.Name.Equals("Shortcutkey"))
                    {
                        if (at.InnerText.Equals(ShortcutKey))
                        {
                            node.RemoveChild(node1);
                            _kt = true;
                        }
                    }
                }
            }
            doc.Save(Filename);
            return _kt;
        }

        #endregion

        #region

        public static bool CreateNewTable(string Filename)
        {
            try
            {
                FileInfo _FileInfo = new FileInfo(Filename);
                XmlTextWriter write = new XmlTextWriter(Filename, null);
                write.Formatting = Formatting.Indented;
                write.WriteStartDocument();
                write.WriteStartElement("", "AutoKey", "");
                write.WriteStartElement("", "Keys", "");
                write.WriteStartAttribute("", "Shortcutkey", "");
                write.WriteString("");
                write.WriteEndAttribute();
                write.WriteStartAttribute("", "Runningkey", "");
                write.WriteString("");
                write.WriteEndAttribute();
                write.WriteStartAttribute("", "Delay", "");
                write.WriteValue(0);
                write.WriteEndAttribute();
                write.WriteStartAttribute("", "Loop", "");
                write.WriteValue(0);
                write.WriteEndAttribute();
                write.WriteStartAttribute("", "Description", "");
                write.WriteValue("");
                write.WriteEndAttribute();
                write.WriteEndElement();
                write.WriteEndElement();
                write.WriteEndDocument();
                write.Flush();
                write.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool KiemTraThem(String ShostcutKey, string Filename)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Filename);
            XmlNode node = doc.DocumentElement;
            //int i = doc.GetElementsByTagName("Keys").Count;
            foreach (XmlNode node1 in node.ChildNodes)
            {
                foreach (XmlAttribute at in node1.Attributes)
                {
                    if (at.Name.Equals("Shortcutkey"))
                    {
                        if (Convert.ToString(at.Value) == ShostcutKey)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        public static bool KiemTraThem(CKeys Key, String Filename)
        {
            return KiemTraThem(Key.Shortcutkey, Filename);
        }

        #endregion

        #endregion

        #region UnStatic

        #region Load Data

        public DataTable LoadListKeys()
        {
            return LoadListKeys(_Filename);
        }

        public CKeys LoadKey(string ShortcutKey)
        {
            return LoadKey(ShortcutKey);
        }

        #endregion

        #region ADD/Edit/Delete

        public bool AddKey(CKeys NewKey)
        {
            return AddKey(NewKey, _Filename);
        }

        public bool EditKey(CKeys KeyNew, String ShortcutKeyOld)
        {
            return EditKey(KeyNew, ShortcutKeyOld, _Filename);
        }

        public bool DeleteKey(String ShortcutKey)
        {
            return DeleteKey(ShortcutKey, _Filename);
        }

        #endregion

        #region

        public bool CreateNewTable()
        {
            return CreateNewTable(_Filename);
        }

        public bool KiemTraThem(String ShostcutKey)
        {
            return KiemTraThem(ShostcutKey, _Filename);
        }

        public bool KiemTraThem(CKeys Key)
        {
            return KiemTraThem(Key, _Filename);
        }

        #endregion

        #endregion
    }
}
